package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class QuestionsActivity extends Activity {

	ArrayList<NameEntity> questions;
	ArrayList<Item> questionItems;
	MyCustomAdapter dataAdapter;
	int course_id = 0;
	String type, condition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_majors);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			course_id = extras.getInt("course_id");
			type = extras.getString("type");
			if (course_id != 0) {
				condition = "?course_id=" + course_id;
			}
			if (type != null) {
				if (type.equals("public"))
					condition = "?public=1";
				else {
					if (sip.isInstructor()) {
						condition = "?i_id=" + sip.getUserId();
					} else
						condition = "?student_id=" + sip.getUserId();

				}

			}
		}
		getquestions();
	}

	public void getquestions() {
		Log.d("ray", "getting: ");
		String serverURL = new myURL("read_questions.php" + condition, true)
				.getURL();
		RZHelper p = new RZHelper(serverURL, this, "setQuestions", true);
		p.get();
		// setQuestions("","");
	}

	public void setQuestions(String s, String error) {
		Log.d("ray", "questionssssssssss: " + s);
		NameEntity ne = null;
		questions = new ArrayList<NameEntity>();
		ArrayList<Question> myCs = new APIManager().getQuestions(s);
		for (int i = 0; i < myCs.size(); i++) {
			Log.d("ray", " Course id" + myCs.get(i).getId() + " ,name: "
					+ myCs.get(i).toString());
			ne = new NameEntity(myCs.get(i).getId(), myCs.get(i).toString(),
					"question");
			ne.setDescription(myCs.get(i).getStudent());
			questions.add(ne);
		}
		questionItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		Item question;
		if (questions.size() == 0) {
			questionItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < questions.size(); i++) {
				question = new Item(questions.get(i).getId(), "", questions
						.get(i).toString());
				question.setCode(questions.get(i).getDescription());
				questionItems.add(question);
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.name_list,
					questionItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						i = new Intent(getBaseContext(), QuestionActivity.class);
						Log.d("ray",
								"calling: "
										+ dataAdapter.tmpList.get(position)
												.getId() + " id:" + id);
						i.putExtra("parent_id",
								dataAdapter.tmpList.get(position).getId());
						startActivity(i);

					}
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		if (sip.isStudent()&& course_id !=0) {
			getMenuInflater().inflate(R.menu.categories, menu);
			SharedMenu.onCreateOptionsMenu(this, menu, getApplicationContext(),
					dataAdapter);
		}
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (sip.isStudent() && course_id !=0) {
			if (SharedMenu.onOptionsItemSelected(item, this) == false) {
				// handle local menu items here or leave blank

				switch (item.getItemId()) {
				case R.id.add:
					Intent intent = new Intent(this, AddQuestionActivity.class);
					intent.putExtra("course_id", course_id);
					startActivity(intent);
					break;
				}
			}
		}
		return super.onOptionsItemSelected(item);
	}

}
